// Copyright (c) 2022-present, Shanghai Yunxi Technology Co, Ltd. All rights reserved.
//
// This software (KWDB) is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

package failpoint

import (
	"context"
	"gitee.com/kwbasedb/kwbase/pkg/roachpb"

	"gitee.com/kwbasedb/kwbase/pkg/kv/kvserver"
	"gitee.com/kwbasedb/kwbase/pkg/util/log"
	"github.com/pingcap/failpoint"
)

func init() {
	kvserver.InjectTestPreSnapPanic = injectTestPreSnapPanic
	kvserver.InjectTestInSnapPanic = injectTestInSnapPanic
	kvserver.InjectTestPostSnapPanic = injectTestPostSnapPanic
}

func injectTestPreSnapPanic(ctx context.Context, tableID uint32, creatingReplica *roachpb.ReplicaDescriptor, from roachpb.NodeID, to roachpb.NodeID) {
	failpoint.Inject("Pre-SnapFailPoint", func() {
		// 345, -5, +1/2, 34(1/2)
		if tableID == 79 && creatingReplica != nil && from == 3 && (to == 1 || to == 2) {
			log.Fatalf(ctx, "Pre-SnapFailPoint panic")
		}
	})
}

func injectTestInSnapPanic(ctx context.Context, tableID uint64, from roachpb.NodeID, to roachpb.NodeID) {
	failpoint.Inject("In-SnapFailPoint", func() {
		// 345, -5, +1/2, 34(1/2)
		if tableID == 79 && from == 3 && (to == 1 || to == 2) {
			log.Fatalf(ctx, "In-SnapFailPoint panic")
		}
	})
}

func injectTestPostSnapPanic(ctx context.Context, tableID uint64, from roachpb.NodeID, to roachpb.NodeID) {
	failpoint.Inject("Post-SnapFailPoint", func() {
		// 345, -5, +1/2, 34(1/2)
		if tableID == 79 && from == 3 && (to == 1 || to == 2) {
			log.Fatalf(ctx, "Post-SnapFailPoint panic")
		}
	})
}
